<?php
/**
 * Created by PhpStorm.
 * User: ruma
 * Date: 2019/1/20
 * Time: 17:46
 */

namespace app\admin\model;


use app\admin\validate\ChangeAdminValidate;
use app\common\model\AdminBaseModel;

class Config extends AdminBaseModel
{
    protected $table = 'admin';

    /**
     * 修改账号
     * @return boolean
     */


    public function changeAdmin(){
        //1.session中获取admin
        //2.获取提交参数
        $postData = (New ChangeAdminValidate())->postCheckAndGet();
        $admin = $postData['admin'];
        $new_admin = $postData['new_admin'];
        $this
            ->where('username','=',$admin)
            ->setField('username',$admin);
        session('admin_name',$new_admin);
    }

    /**
     * 修改密码
     * @return boolean
    */
//    public function changePassword(){
//        //1.参数验证，返回正确或错误数组
//        $postData = (new ChangePwdValidate())->postCheckAndGet();
//
//        //2.成功获取post的新旧密码
//        $pwd = $postData['password'];
//        $newPwd = $postData['new_password'];
//
//        //3.设置比对密码与之前是否一致
//        $adminName = session('admin_name');
//        $userInfo = $this->getUserInfoByUsername($adminName);
//        if (!$userInfo){
//            throw new ParameterException([
//                'msg' => '没有该用户信息'
//            ]);
//        }
//
//        //4.密码验证
//        $bool = passwordVerify($pwd, $userInfo['password']);
//        if (!$bool){
//            throw new ParameterException([
//                'msg' => '原始密码错误'
//            ]);
//        }
//
//        //5.密码正确设置密码
//        $result = $this->where('username','=',$adminName)->setField('password', password_hash($newPwd,PASSWORD_BCRYPT));
//        if (!$result){
//            throw new DatabaseException([
//                'msg' => '数据库添加异常'
//            ]);
//        }
//        return true;
//    }
}